package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.data.ResGoodsCreateData;
import com.cskaoyan.bean.data.ResGoodsDetailData;
import com.cskaoyan.bean.data.ResGoodsUpdateData;
import com.cskaoyan.bean.data.RespCatAndBrandData;
import com.cskaoyan.bean.vo.GoodsVo;
import com.cskaoyan.service.GoodsService;
import com.cskaoyan.service.GoodsServiceImpl;
import com.cskaoyan.util.JacksonUtil;
import lombok.SneakyThrows;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin/goods/*")
public class AdminGoodsServlet extends WdBaseServlet {
    @SneakyThrows
    public BaseRespVo list(HttpServletRequest request, HttpServletResponse response){
        GoodsService goodsService = new GoodsServiceImpl();
        //获取参数
        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");
        String goodSn = null;
        if(request.getParameter("goodsSn")!=null&&!"".equals(request.getParameter("goodsSn"))){
            goodSn = request.getParameter("goodsSn");
        }
        String name = null;
        if(request.getParameter("name")!=null&&!"".equals(request.getParameter("name"))){
            name = request.getParameter("name");
        }
        Integer goodsId = null;
        if(request.getParameter("goodsId")!=null&&!"".equals(request.getParameter("goodsId"))){
            goodsId = Integer.parseInt(request.getParameter("goodsId"));
        }

        CommonData<MarketGoods> commonData = goodsService.list(page, limit, sort, order,goodSn,name,goodsId);
        return BaseRespVo.ok(commonData);
    }

    public BaseRespVo update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        GoodsService goodsService = new GoodsServiceImpl();
        //获取参数
        String jsonStr = request.getReader().readLine();
        ResGoodsUpdateData data = JacksonUtil.read(jsonStr, ResGoodsUpdateData.class);

        goodsService.update(data);
        //wyy修改以下一行，为日志服务
        return BaseRespVo.ok(data);
    }

    @SneakyThrows
    public BaseRespVo delete(HttpServletRequest request, HttpServletResponse response) {
        GoodsService goodsService = new GoodsServiceImpl();
        String jsonStr = request.getReader().readLine();
        MarketGoods goods = JacksonUtil.read(jsonStr, MarketGoods.class);

        goodsService.delete(goods);

        //wyy修改以下一行，为日志服务
        return BaseRespVo.ok(goods);
    }

    public BaseRespVo detail(HttpServletRequest request, HttpServletResponse response){
        GoodsService goodsService = new GoodsServiceImpl();
        Integer id = Integer.parseInt(request.getParameter("id"));
        ResGoodsDetailData detail = goodsService.detail(id);

    return BaseRespVo.ok(detail);
    }

    public BaseRespVo catAndBrand(HttpServletRequest request, HttpServletResponse response){
        GoodsService goodsService = new GoodsServiceImpl();
        RespCatAndBrandData respCatAndBrandData = goodsService.catAndBrand();
        return BaseRespVo.ok(respCatAndBrandData);
    }

    @SneakyThrows
    public BaseRespVo create(HttpServletRequest request, HttpServletResponse response){
        GoodsService goodsService = new GoodsServiceImpl();
        //接收参数
        String jsonStr = request.getReader().readLine();
        GoodsVo data = JacksonUtil.read(jsonStr, GoodsVo.class);
        if(data.getGoods().getGoodsSn()==null){
            return BaseRespVo.topicNo();
        }
        if(data.getGoods().getName()==null){
            return BaseRespVo.topicNo();
        }
        BaseRespVo baseRespVo = goodsService.create(data);
        return baseRespVo;
       /* if(f==true){
            return BaseRespVo.ok(data);
        }else {
            return BaseRespVo.exist();
        }*/
        //wyy修改以下一行，为日志服务
    }
}
